package com.siqi.hr.system.common.utils;

import com.siqi.hr.system.common.sql.Condition;
import com.siqi.hr.system.common.sql.SField;
import com.siqi.hr.system.common.sql.SFieldType;

public class SqlBuildUtil {
	
	public static StringBuilder buildupColumuOption(SField field, int length2, boolean nullable2, String defaultValue2, Integer precision) {
		StringBuilder sb = new StringBuilder();
		sb.append(field.getName()).append(" ");

		if ((field.isQuote()) && (field.getName().split("_")[1].equals("sys"))) {
			sb.append(SFieldType.DatabaseType.INT.getDataBaseType());
			length2 = 11;
		} else {
			sb.append(field.getSFieldType().getDatabaseType().getDataBaseType());
		}

		if ((field.getSFieldType().getDatabaseType() != SFieldType.DatabaseType.DATETIME)
				&& (field.getSFieldType().getDatabaseType() != SFieldType.DatabaseType.TIMESTAMP)
				&& (field.getSFieldType().getDatabaseType() != SFieldType.DatabaseType.DATE)
				&& (field.getSFieldType().getDatabaseType() != SFieldType.DatabaseType.BIT)) {
			sb.append("(").append(length2);
			if (field.getSFieldType().getDatabaseType() == SFieldType.DatabaseType.DECIMAL) {
				sb.append("," + precision);
			}
			sb.append(")");
		}

		if ((defaultValue2 != null) && (!defaultValue2.equals(""))) {
			sb.append(" default ").append(
					surroundValue(field.getSFieldType(), defaultValue2));
		}
		return sb;
	}

	public static String surroundValue(SFieldType sFieldType, String value) {
		if (sFieldType.getDatabaseType().needSingleQuotationMarks()) {
			String s = sFieldType.getDatabaseType().getCode();
			if ((s.equals("decimal")) && (value.equals(""))) {
				return "'0'";
			}

			if (value.equals("")) {
				return "null";
			}

			return "'" + value + "'";
		}

		if (value.equals("")) {
			return "null";
		}
		return value;
	}

	public static String surroundValue(SFieldType sFieldType, String value,
			String fieldName) {
		if (sFieldType.getDatabaseType().needSingleQuotationMarks()) {
			String s = sFieldType.getDatabaseType().getCode();
			if ((s.equals("decimal")) && (value.equals(""))) {
				return "'0'";
			}
			if ((fieldName.indexOf("pk_") != -1) && (value.equals(""))) {
				return "null";
			}
			return "'" + value + "'";
		}

		if (value.equals("")) {
			return "null";
		}
		return value;
	}

	public static String surroundValue(SFieldType sFieldType, String value,
			Condition.ConditionType type) {
		Boolean exclude = Boolean.valueOf((!Condition.ConditionType.in
				.equals(type))
				&& (!Condition.ConditionType.notin.equals(type))
				&& (!Condition.ConditionType.isnull.equals(type))
				&& (!Condition.ConditionType.notnull.equals(type)));

		if ((sFieldType.getDatabaseType().needSingleQuotationMarks())
				&& (exclude.booleanValue())) {
			return "'" + value + "'";
		}

		return value;
	}

	public static void main(String[] args) {
		System.out.println(SFieldType.DatabaseType.INT.getDataBaseType());
	}
}
